﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Repository;

namespace ZFB.WebAPI.Controllers
{
    /// <summary>
    /// 房源
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HouseController : ControllerBase
    {
        private readonly IHouseServices _houseServices;

        public HouseController(IHouseServices houseServices)
        {
            _houseServices = houseServices;
        }
        /// <summary>
        /// 根据id获取房子
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetById(long id) 
        {
            return await _houseServices.GetByIdAsync(id);
        }
        /// <summary>
        /// 获取房源类别下的城市中的房源总数量
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetTotalCount(long cityId, long typeId) 
        {
            return await _houseServices.GetTotalCountAsync(cityId, typeId);
        }
        /// <summary>
        /// 分页获取房源类别下的城市中的房源
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="typeId"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetPageDate(long cityId, long typeId, int page, int limit) 
        {
            return await _houseServices.GetPageDateAsync(cityId, typeId, page, limit);
        }
        /// <summary>
        /// 新增房源返回Id
        /// </summary>
        /// <param name="addhouseDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddNew(AddHouseDTO addhouseDTO) 
        {
            return await _houseServices.AddNewAsync(addhouseDTO);
        }
        /// <summary>
        /// 更新房源，房源附件先删除再新增
        /// </summary>
        /// <param name="editDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> Update(EditHouseDTO editDTO) 
        {
            return await _houseServices.UpdateAsync(editDTO);
        }
        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> MarkDelete(long id) 
        {
            return await _houseServices.MarkDeleteAsync(id);
        }
        /// <summary>
        /// 得到房源图片
        /// </summary>
        /// <param name="houseId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetPics(long houseId) 
        {
            return await _houseServices.GetPicsAsync(houseId);
        }
        /// <summary>
        /// 添加房源图片返回id
        /// </summary>
        /// <param name="housePicDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddPics(HousePicDTO housePicDTO) 
        {
            return await _houseServices.AddPicsAsync(housePicDTO);
        }
        /// <summary>
        /// 图片软删除
        /// </summary>
        /// <param name="housePicId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeletePic(long housePicId) 
        {
            return await _houseServices.DeletePicAsync(housePicId);
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="houseSearchOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SearchAsync(HouseSearchOptions houseSearchOptions) 
        {
            return null;
        }
        /// <summary>
        /// 获取时间段内的房子总数
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="startDateTime"></param>
        /// <param name="endDateTime"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetCount(long cityId, DateTime startDateTime, DateTime endDateTime) 
        {
            return await _houseServices.GetCountAsync(cityId, startDateTime, endDateTime);
        }
        /// <summary>
        /// 获取房子的配套设施
        /// </summary>
        /// <param name="houseId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetAttachments(long houseId) 
        {
            return await _houseServices.GetAttachmentsAsync(houseId);
        }
    }
}
